package com.wissen.giskard.client.widget;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;

import com.wissen.giskard.client.controller.LoginController;
import com.wissen.giskard.client.observer.LoginObserver;
import com.wissen.giskard.client.payroll.widget.LoadTable;


/* * 
 * 
 * @author Swapnil sonawane
 *
 */
/*
 *      Loginwidget load the login window  
 */

public class LoginWidget extends Composite implements LoginObserver {
	
	private  AbsolutePanel   mainabsPanel      =  new AbsolutePanel();
	
	private  AbsolutePanel   loginPanel      =  new AbsolutePanel();
	
	private  TextBox         userNameText    =  new TextBox();
	
	private  PasswordTextBox passwordText    =  new PasswordTextBox();
	
	private  Label           userNameLabel   =  new Label("User Name ");
	
	private  Label           passwordLabel   =  new Label("Password ");
	
	private   Button      signInButton    =  new Button("Sign In");
	
	private  Button          signUpButton    =  new Button("Sign Up");
	
	private Label            warningLabel    =  new Label();
	
	private Label            errLabel        =  new Label();
	
	/*
	 *  Constructor of LoginWidget
	 */
	public LoginWidget()
	{
		init();
		
		LoginController.getInstance().addLoginObserver(this);
		
		signInButton.addClickHandler( new buttonClickHandler());
		userNameText.addKeyUpHandler(new keyupHandler());
		passwordText.addKeyUpHandler(new keyupHandler());
		
		initWidget(mainabsPanel);
			
	}
	/*
	 *   this method Initialise all widgets
	 */
	
	private void init()
	{
		
		mainabsPanel.setSize("300px", "170px");
		mainabsPanel.addStyleName("panel");
		
		userNameText.setWidth("120px");
		passwordText.setWidth("120px");
		
		
		loginPanel.setSize("250px","160px");
		loginPanel.add(userNameLabel, 0, 10);
		loginPanel.add(userNameText, 90, 10);
		loginPanel.add(passwordLabel, 0, 40);
		loginPanel.add(passwordText, 90, 40);
		loginPanel.add(signInButton, 0, 70);
		loginPanel.add(signUpButton,90, 70);
		loginPanel.add(warningLabel,0,100);
		
		mainabsPanel.add(loginPanel,60,40);
		
	}
	
	
	/*
	 *  clear warning message 
	 */
	public class keyupHandler implements KeyUpHandler{

		@Override
		public void onKeyUp(KeyUpEvent event) {
			warningLabel.setText("");
			
		}
		
	}
	
	/*
	 *  check for valid user
	 * 
	 */
	
	public class buttonClickHandler implements ClickHandler {

		

		

		@Override
		public void onClick(ClickEvent event) {
			if(userNameText.getText().equals("")){
				warningLabel.setText("Please Enter user name");
			}
			else if(passwordText.getText().equals("")){
			    warningLabel.setText("Please Enter password");
			}
			else 
			{
				LoginController.getInstance().validateUser(userNameText.getText(), passwordText.getText());
		    }
			
		}
		
	}

	@Override
	public void notifyLoginFail() {
		errLabel.setText("error in connting");
		mainabsPanel.clear();
		mainabsPanel.add(errLabel, 50,70);
		
	}
	@Override
	public void notifyLoginSuccess(boolean result) {
		if(result){
			warningLabel.setText("login success");
			mainabsPanel.setSize("520px", "330px");
			mainabsPanel.clear();
			mainabsPanel.add(new LoadTable());
		}
		else{
			warningLabel.setText("Invalid Username and password");
			
		}
	}
	
}
